package com.nucc.control;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.testng.annotations.Test;

import com.nucc.model.FinishInfo;
import com.nucc.model.RunInfo;
import com.nucc.service.FastTestJenkinsService;  

@Controller  
@RequestMapping("/")  
public class HelloWorldController {  

 @RequestMapping(method = RequestMethod.GET)  
 public String sayHello(ModelMap model) {  
     model.addAttribute("greeting", "Hello World from Spring 4 MVC");  
     return "welcome";  
 }  

 @RequestMapping(value="/helloagain", method = RequestMethod.GET)  
 public String sayHelloAgain(ModelMap model) {  
     model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");  
     return "welcome";  
 }  
 
 @Test
 @RequestMapping(value = "/fasttest/run.json", method = RequestMethod.POST)
 public RunInfo runFasttest(@RequestParam(value = "env", required = true) String env,
                     @RequestParam(value = "module", required = true, defaultValue = "BVT") String module,
                     @RequestParam(value = "email", required = true, defaultValue = "hugang") String email){

     FastTestJenkinsService fastTestJenkinsService = new FastTestJenkinsService();
    /* // 增加邮箱后缀
     EmailSuffixService emailSuffix = new EmailSuffixService();

     String receiver = emailSuffix.addEmailSuffix(email);*/
     String receiver = "380724532@163.com";

     int jobId = fastTestJenkinsService.buildJob(env, module, receiver);

     RunInfo runInfo = new RunInfo();

     runInfo.setJobId(jobId);
     runInfo.setResult(true);

     return runInfo;

 }
 
 /**
  * 根据jobId判断是否执行结束接口
  * @param job_id
  * @return
  */
 
 @RequestMapping(value = "/fasttest/finish.json",method = RequestMethod.GET)
 public @ResponseBody
 FinishInfo isFinish(@RequestParam(value = "job_id", required = true)int job_id) {

     FastTestJenkinsService fastTestJenkinsService = new FastTestJenkinsService();

     boolean isFinish = fastTestJenkinsService.isJobFinish(job_id);

     FinishInfo finishInfo = new FinishInfo();

     finishInfo.setFinished(isFinish);

     return finishInfo;

 }

}  